package analyse;

import java.math.BigDecimal;
import java.util.List;

import util.DateUtil;

import com.stock.dao.Averline;

import data.Constant;

public class LowestPriceAlyz  extends AbstractAlyzer{

	
	public LowestPriceAlyz() {
	}

	@Override
	DealDayData alanalyze(List<DealDayData> dealDatas) {
		Averline averline = new Averline();
		BigDecimal lowPrice = new BigDecimal("100000");
		DealDayData result = new DealDayData();
		BigDecimal priceBat = new BigDecimal("1");//除权
//		BigDecimal averPrice = new BigDecimal("0");
		
		BigDecimal fiveDayPrice = Constant.ZERO;
		BigDecimal tenDayPrice = Constant.ZERO;
		BigDecimal fifteenDayPrice = Constant.ZERO;
		BigDecimal twentyDayPrice = Constant.ZERO;
		BigDecimal sixtyPrice = Constant.ZERO;
		BigDecimal thirtyPrice = Constant.ZERO;
		BigDecimal fourtyPrice = Constant.ZERO;
		
		boolean lowSign = false;//连跌天数
		int fallDays = 0;
		
		for (DealDayData dealDayData : dealDatas) {
			if(dealDayData.getRatePercent().compareTo(new BigDecimal("-10.5"))<0){
				priceBat = priceBat.add(dealDayData.getRatePercent().divide(Constant.ONEHUNDRED));
			}
			dealDayData.setMinPrice(dealDayData.getMinPrice().multiply(priceBat).setScale(2,BigDecimal.ROUND_HALF_UP));
			
			if(lowPrice.compareTo(dealDayData.getMinPrice())>0){
				lowPrice = dealDayData.getMinPrice();
				result = dealDayData;
			}
			
			if(!lowSign&&dealDayData.getRatePercent().compareTo(Constant.ZERO)>0){
				fallDays = dealDayData.getSeq()-1;
				lowSign = true;
			}
			
			if(dealDayData.getSeq()<=60){
				sixtyPrice = sixtyPrice.add(dealDayData.getPrice());
				if(dealDayData.getSeq()<=40){
					fourtyPrice = fourtyPrice.add(dealDayData.getPrice());
					if(dealDayData.getSeq()<=30){
						thirtyPrice = thirtyPrice.add(dealDayData.getPrice());
						if(dealDayData.getSeq()<=20){
							twentyDayPrice = twentyDayPrice.add(dealDayData.getPrice());
							if(dealDayData.getSeq()<=15){
								fifteenDayPrice = fifteenDayPrice.add(dealDayData.getPrice());
								if(dealDayData.getSeq()<=10){
									tenDayPrice = tenDayPrice.add(dealDayData.getPrice());
									if(dealDayData.getSeq()<=5){
										fiveDayPrice = fiveDayPrice.add(dealDayData.getPrice());
									}
								}
							}
						}
					}
				}
			}
			
		}
		
		if(priceBat.compareTo(new BigDecimal("1"))!=0){
			result.setPriceDeal(true);
		}
		
		if(dealDatas.get(0).getPrice().subtract(result.getMinPrice()).divide(result.getMinPrice(), 2,BigDecimal.ROUND_HALF_UP)
				.compareTo(new BigDecimal(0.08))<0){
			result.setPriceLevel(-1);
		}
		
		result.setFallDays(fallDays);
		result.setTwentyDayPrice(twentyDayPrice.divide(new BigDecimal("20"),2,BigDecimal.ROUND_HALF_UP));
		result.setThirtyDayPrice(thirtyPrice.divide(new BigDecimal("30"),2,BigDecimal.ROUND_HALF_UP));
		
		averline.setNumber(result.getNumber());
		averline.setDealdate(dealDatas.get(0).getDate());
		averline.setAver5(fiveDayPrice.divide(new BigDecimal("5"),2,BigDecimal.ROUND_HALF_UP));
		averline.setAver10(tenDayPrice.divide(new BigDecimal("10"),2,BigDecimal.ROUND_HALF_UP));
		averline.setAver15(fifteenDayPrice.divide(new BigDecimal("15"),2,BigDecimal.ROUND_HALF_UP));
		averline.setAver20(twentyDayPrice.divide(new BigDecimal("20"),2,BigDecimal.ROUND_HALF_UP));
		averline.setAver30(thirtyPrice.divide(new BigDecimal("30"),2,BigDecimal.ROUND_HALF_UP));
		averline.setAver40(fourtyPrice.divide(new BigDecimal("40"),2,BigDecimal.ROUND_HALF_UP));
		averline.setAver60(sixtyPrice.divide(new BigDecimal("60"),2,BigDecimal.ROUND_HALF_UP));
		result.setAverline(averline);
		
		return result;
	}
}
